package com.example.springsecurity.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.springsecurity.domain.Announcement;
import com.example.springsecurity.domain.News;
import com.example.springsecurity.domain.UploadCentre;
import com.example.springsecurity.service.ExperimentPortalService;
import com.example.springsecurity.utils.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 门户网站接口
 */
@RestController
@RequestMapping(value = "/experimentPortal")
public class ExperimentPortalController {



    @Autowired
    public ExperimentPortalService experimentPortalService;


    /**
     * 门户网站根据学院编号来获取学院信息
     * @param getPortalExperiment
     * @return
     */
    @RequestMapping(value = "/getPortalExperiment",method = RequestMethod.POST)
    public ResultBean getPortalExperiment(@RequestBody String getPortalExperiment){
        JSONObject jsonObject= JSON.parseObject(getPortalExperiment);
        Integer collegeId=jsonObject.getInteger("collegeId");
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        String projectName=jsonObject.getString("projectName");
        return experimentPortalService.getPortalExperiment(collegeId,pageNum,pageSize,projectName);
    }


    /**
     * 门户网站根据年份和月份来获取新闻的详细信息
     * @param getNews
     * @return
     */
    @RequestMapping(value = "/getNews",method = RequestMethod.POST)
    public ResultBean getNews(@RequestBody String getNews){
        JSONObject jsonObject=JSON.parseObject(getNews);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        Integer year=jsonObject.getInteger("year");
        Integer month=jsonObject.getInteger("month");
        String newsTitle=jsonObject.getString("newsTitle");
        return experimentPortalService.getNews(pageNum,pageSize,year,month,newsTitle);
    }


    /**
     * 根据编号来获取新闻的详细信息
     * @param data
     * @return
     */
    @RequestMapping(value = "/getNewsDetail",method = RequestMethod.POST)
    public ResultBean getNewsDetail(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        Integer newsId=jsonObject.getInteger("newsId");
        return experimentPortalService.getNewsDetail(newsId);
    }



    /**
     * 门户网站添加新闻
     * @param news
     * @return
     */
    @RequestMapping(value = "/addNews",method = RequestMethod.POST)
    public ResultBean addNews(@RequestBody News news){
        return experimentPortalService.addNews(news);
    }


    /**
     * 修改新闻信息
     * @param news
     * @return
     */
    @RequestMapping(value = "/updateNews",method = RequestMethod.POST)
    public ResultBean updateNews(@RequestBody News news){
        return experimentPortalService.updateNews(news);
    }

    /**
     * 门户网站获取到公告信息
     * @param getAnnouncement
     * @return
     */
    @RequestMapping(value = "/getAnnouncement",method = RequestMethod.POST)
    public ResultBean getAnnouncement(@RequestBody String getAnnouncement){
        JSONObject jsonObject=JSON.parseObject(getAnnouncement);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        Integer year=jsonObject.getInteger("year");
        Integer month=jsonObject.getInteger("month");
        String announcementTitle=jsonObject.getString("announcementTitle");
        return experimentPortalService.getAnnouncement(pageNum,pageSize,year,month,announcementTitle);
    }


    /**
     * 添加公告
     * @param announcement
     * @return
     */
    @RequestMapping(value = "/addAnnouncement",method = RequestMethod.POST)
    public ResultBean addAnnouncement(@RequestBody Announcement announcement){
        return experimentPortalService.addAnnouncement(announcement);
    }


    /**
     * 修改公告信息
     * @param announcement
     * @return
     */
    @RequestMapping(value = "/updateAnnouncement",method = RequestMethod.POST)
    public ResultBean updateAnnouncement(@RequestBody Announcement announcement){
        return experimentPortalService.updateAnnouncement(announcement);
    }


    /**
     * 根据编号来获取公告信息
     * @param data
     * @return
     */
    @RequestMapping(value = "/getAnnouncementId",method = RequestMethod.POST)
    public ResultBean getAnnouncementId(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        Integer announcementId=jsonObject.getInteger("announcementId");
        return experimentPortalService.getAnnouncementId(announcementId);
    }


    /**
     * 获取到下载信息
     * @param data
     * @return
     */
    @RequestMapping(value = "/getUploadInfo",method = RequestMethod.POST)
    public ResultBean getUploadInfo(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        Integer pageNum=jsonObject.getInteger("pageNum");
        Integer pageSize=jsonObject.getInteger("pageSize");
        Integer uploadCategory=jsonObject.getInteger("uploadCategory");
        String uploadIntroduce=jsonObject.getString("uploadIntroduce");//模糊查询下载工具介绍
        return experimentPortalService.getUploadInfo(pageNum,pageSize,uploadCategory,uploadIntroduce);
    }


    /**
     * 修改下载中心编号
     * @param uploadCentre
     * @return
     */
    @RequestMapping(value = "/updateUploadInfo",method = RequestMethod.POST)
    public ResultBean updateUploadInfo(@RequestBody UploadCentre uploadCentre){
        return experimentPortalService.updateUploadInfo(uploadCentre);
    }


    /**
     * 上传工具下载信息
     * @param file
     * @param uploadCentre
     * @return
     */
    @RequestMapping(value = "/addUploadInfo",method = RequestMethod.POST)
    public ResultBean addUploadInfo(MultipartFile file,UploadCentre uploadCentre){
        return  experimentPortalService.addUploadInfo(file,uploadCentre);
    }

    /**
     *删除新闻
     * @param data
     * @return
     */
    @RequestMapping(value = "/deleteNews",method = RequestMethod.POST)
    public ResultBean deleteNews(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        String  newsId=jsonObject.getString("newsId");
        return experimentPortalService.deleteNews(newsId);
    }


    /**
     * 删除公告
     * @return
     */
    @RequestMapping(value = "/deleteAnnouncement",method = RequestMethod.POST)
    public ResultBean deleteAnnouncement(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        String announcementId=jsonObject.getString("announcementId");
        return experimentPortalService.deleteAnnouncement(announcementId);
    }


    /**
     * 下载下载工具
     * @param uploadCentreId
     */
    @RequestMapping(value = "/downloadInfo",method = RequestMethod.GET)
    public void downloadInfo(Integer uploadCentreId, HttpServletRequest request, HttpServletResponse response){
         experimentPortalService.downloadInfo(uploadCentreId,request,response);
    }


    /**
     * 删除下载工具
     * @param data
     * @return
     */
    @RequestMapping(value = "/deleteDownTool",method = RequestMethod.POST)
    public ResultBean deleteDownTool(@RequestBody String data){
        JSONObject jsonObject=JSON.parseObject(data);
        String uploadCentreId=jsonObject.getString("uploadCentreId");
        return experimentPortalService.deleteDownTool(uploadCentreId);
    }

}
